﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PRO.Common
{
    /// <summary>
    /// 字符串处理扩展类
    /// </summary>
    public static class StringUtils
    {
        /// <summary>
        /// 判断字符串是否是Decimal类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDecimal(this object str)
        {
            if (str == null)
                return false;
            decimal r = -1;
            return decimal.TryParse(str.ToString(), out r);
        }
        /// <summary>
        /// 字符串转成decimal类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultval">缺省值</param>
        /// <returns></returns>
        public static decimal ToDecimal(this object str, decimal defValue)
        {
            if (str != null)
            {
                decimal r = defValue;
                if (!decimal.TryParse(str.ToString(), out r))
                {
                    return defValue;
                }
                return r;
            }
            else
                return defValue;
        }
        /// <summary>
        /// 判断字符串是否是Int32类型
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsInt32(this object Expression)
        {
            if (Expression != null)
            {
                string str = Expression.ToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 字符串转换成Int类型
        /// </summary>
        /// <param name="Expression"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns></returns>
        public static int ToInt(this object Expression, int defValue)
        {
            if (Expression != null)
            {
                string str = Expression.ToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                    {
                        return Convert.ToInt32(str);
                    }
                }
            }
            return defValue;
        }
        /// <summary>
        /// 将字符串转换位Boolean类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns></returns>
        public static bool ToBoolean(this object str, bool defValue)
        {
            if (str != null)
            {
                if (string.Compare(str.ToString(), "true", true) == 0)
                {
                    return true;
                }
                else if (string.Compare(str.ToString(), "false", true) == 0)
                {
                    return false;
                }
            }
            return defValue;
        }

        /// <summary>
        /// 字符串转换为Float类型
        /// </summary>
        /// <param name="strValue"></param>
        /// <param name="defValue">缺省值</param>
        /// <returns></returns>
        public static float ToFloat(this object strValue, float defValue)
        {
            if ((strValue == null) || (strValue.ToString().Length > 10))
            {
                return defValue;
            }

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                {
                    intValue = Convert.ToSingle(strValue);
                }
            }
            return intValue;
        }
        /// <summary>
        /// 判断字符串是否是Float类型
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static bool IsFloat(this object strValue)
        {
            if ((strValue == null) || (strValue.ToString().Length > 10))
            {
                return false;
            }
            if (strValue != null)
            {
                return Regex.IsMatch(strValue.ToString(), @"^([-]|[0-9])[0-9]*(\.\w*)?$");
            }
            else
                return false;
        }
        /// <summary>
        /// 对字符串进行URL编码
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string UrlEncode(this object strValue)
        {
            if (strValue != null)
            {
                if (string.IsNullOrWhiteSpace(strValue.ToString()))
                    return "";
                return System.Web.HttpUtility.UrlEncode(strValue.ToString());
            }
            else
                return null;
        }
        /// <summary>
        /// 对字符串进行URL解码
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string UrlDecode(this object strValue)
        {
            if (strValue != null)
            {
                if (string.IsNullOrWhiteSpace(strValue.ToString()))
                    return "";
                return System.Web.HttpUtility.UrlDecode(strValue.ToString());
            }
            return null;
        }
        /// <summary>
        /// 判断字符串是否是email格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(this object str)
        {
            if (str != null)
            {
                if (string.IsNullOrWhiteSpace(str.ToString()))
                    return false;
                return Regex.IsMatch(str.ToString(), @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            }
            return false;
        }
        /// <summary>
        /// 判断字符串是否是URL格式
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static bool IsURL(this object strUrl)
        {
            if (strUrl != null)
            {
                return Regex.IsMatch(strUrl.ToString(), @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
            }
            else
                return false;
        }
        /// <summary>
        /// 判断是否是以字母数字和下划线组成的字符串
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool IsUserName(this object username)
        {
            if (username != null)
            {
                return Regex.IsMatch(username.ToString(), @"^\w+$");
            }
            else
                return false;
        }
        /// <summary>
        /// 判断知否串是否为空或空白字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 转换指定的字符串，以使用 % 字符对保留字符（@、*、_、+、-、. 和 /）进行转义，并以 Unicode 表示法表示它们。 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Escape(this object str)
        {
            if (str != null)
            {
                if (string.IsNullOrWhiteSpace(str.ToString()))
                    return null;
                else
                    return Microsoft.JScript.GlobalObject.escape(str);
            }
            else
                return null;
        }
        /// <summary>
        /// 将指定字符串中 % 的已转义字符（@、*、_、+、-、. 和 /）转换成其原始格式。已转义字符以 Unicode 表示法表示。 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Unescape(this object str)
        {
            if (str != null)
            {
                if (string.IsNullOrWhiteSpace(str.ToString()))
                    return null;
                else
                    return Microsoft.JScript.GlobalObject.unescape(str);
            }
            else
                return null;
        }
        /// <summary>
        /// 出去字符串里的html标记
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string RemoveHTML(this object obj)
        {
            if (obj != null)
                return Utils.RemoveHtml(obj.ToString());
            else
                return null;
        }
        /// <summary>
        /// 过滤掉sql语句里的危险字符
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SqlFilter(this object obj)
        {
            if (obj != null)
            {
                return Utils.SqlFilter(obj.ToString());
            }
            else
                return null;
        }
        /// <summary>
        /// 存入数据库时进行转换，防止注入js代码
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string HtmlChange(this object obj)
        {
            if (obj != null)
            {
                return Utils.HtmlChange(obj.ToString());
            }
            else
                return null;
        }

        /// <summary>
        /// DEC加密
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key">加密向量</param>
        /// <returns></returns>
        public static string DEC_Encode(this object obj, string key)
        {
            if (obj != null)
            {
                return DEC.Encode(obj.ToString(), key);
            }
            else
                return null;
        }
        /// <summary>
        /// DEC解密
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key">加密向量</param>
        /// <returns></returns>
        public static string DEC_Decode(this object obj, string key)
        {
            if (obj != null)
            {
                return DEC.Decode(obj.ToString(), key);
            }
            else
                return null;
        }
        /// <summary>
        /// 验证字符串是否是价格格式：两位小数的正实数
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public static bool IsPrice(this string price)
        {
            Regex regex = new Regex("^[0-9]+(.[0-9]{0,2})?$");
            return regex.IsMatch(price.Trim());
        }
        /// <summary>
        /// 判断是否是纯数字的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumber(this object str)
        {
            if (str != null)
            {
                Regex regex = new Regex(@"^[0-9]+$");
                return regex.IsMatch(str.ToString());
            }
            return false;
        }
    }
}